import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class HumanPlayer implements IPlayer,
									MouseListener,
									MouseMotionListener
{ 
	private String _name;
	private Color _color;
	private IBoard _board;
	
    public HumanPlayer()
	{
		this(null);
	}
	
    public HumanPlayer(String name)
	{
		_name = name;
	}
	
	public void joinBoard(IBoard board, Color color)
	{
		_board = board;
		_color = color;
	}

	public void beginTurn()
    {
		IGui.get().addListener(this);
	}

    public void endTurn()
    {
		IGui.get().removeListener(this);
    }

    public Color getColor()
    {
		return _color;
	}

    public String getName()
    {
		if (_name == null)
			_name = IGui.colorName(_color) + "-Human";
		return _name;
	}
	
	public void mouseClicked(MouseEvent e)
	{
		Point pt = IGui.get().getSquare(e.getPoint());
		if (pt != null)
			_board.placePiece(HumanPlayer.this, pt.y, pt.x);
		else if (IGui.get().isPassButton(e.getPoint()))
			_board.pass(HumanPlayer.this);
	}

    public void mouseEntered(MouseEvent e)
    {
		
	}

    public void mouseExited(MouseEvent e)
    {
    }

    public void mousePressed(MouseEvent e)
    {
    }

    public void mouseReleased(MouseEvent e)
    {
    }

    public void mouseDragged(MouseEvent e)
    {
    }

    public void mouseMoved(MouseEvent e)
    {
		Point pt = IGui.get().getSquare(e.getPoint());
		if (pt != null && _board.isValidMove(getColor(), pt.y, pt.x))
		{
			IGui.get().showDropIndicator(getColor(), pt.y, pt.x);
		}
		else if (IGui.get().isPassButton(e.getPoint()))
		{
			// Special code meaning the Pass button should be highlighted.
			IGui.get().showDropIndicator(null, _board.getRowCount(), -1);
		}
		else
			IGui.get().showDropIndicator(null, -1, -1);
    }
} 
